import store from "./store";
import { getModules } from "./arcgisAPI";

export const searchFeatures = async (url: string, where?: string, geometry?: any) => {
  const modules = await getModules([
    "esri/tasks/QueryTask",
    "esri/tasks/support/Query"
  ]);
  const promise = new Promise<any[]>((resolve, reject) => {
    const [QueryTask, Query] = modules;
    const task = new QueryTask({ url });
    const query = new Query();
    query.returnGeometry = true;
    query.outFields = ["*"];
    query.where = where || "1=1";
    query.geometry = geometry || null
    query.outSpatialReference = store.spatialReference;
    task.execute(query).then((res: any) => {
      resolve(res.features);
    });
  });
  return promise;
};
export const searchFields = async (url: string) => {
  const modules = await getModules([
    "esri/tasks/QueryTask",
    "esri/tasks/support/Query"
  ]);
  const promise = new Promise<any[]>((resolve, reject) => {
    const [QueryTask, Query] = modules;
    const task = new QueryTask({ url });
    const query = new Query();
    query.returnGeometry = true;
    query.outFields = ["*"];
    query.where = "1=1";
    query.outSpatialReference = store.spatialReference;
    task.execute(query).then((res: any) => {
      resolve(res.fields);
    });
  });
  return promise;
};


